#include "PointCloudRenderer.h"
#ifdef RENDER_OPENGL
    #include <gl\GL.h>
#endif
#include "../Scene/ZDataManager.h"
#include "../Common/ZException.h"

GLOBAL_NAMESPACE_BEGIN

PointCloudRenderer::PointCloudRenderer()
{

}

PointCloudRenderer::~PointCloudRenderer()
{

}

void PointCloudRenderer::beginToDraw()
{
#ifdef RENDER_OPENGL
    if (getRenderOption().translucent)
    {
        glEnable(GL_BLEND);
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBlendFunc(GL_ONE, GL_ONE);
        glDisable(GL_DEPTH_TEST);
    }
#endif
}

void PointCloudRenderer::endDraw()
{
#ifdef RENDER_OPENGL
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
#endif
}

void PointCloudRenderer::draw()
{
    MeshSpace::PointCloud* mesh = DataManager::getSingleton().getPointCloud();
    if (!mesh) return;

    beginToDraw();
    // draw something
    drawPointCloud(mesh);
    endDraw();
}

void PointCloudRenderer::drawPointCloud(MeshSpace::PointCloud* pc)
{
#ifdef RENDER_OPENGL
    glEnable(GL_LIGHTING);
    glEnable(GL_POINT_SMOOTH);
    glShadeModel(GL_SMOOTH);
    glBegin(GL_POINTS);
    glPointSize(2.0);
    for (int i = 0; i<pc->getPointSize(); i++)
    {
        MeshSpace::PcPoint point = pc->getPointAt(i);
        glColor4fv(point.color);
        glVertex3dv(point.pos);
    }
    glPointSize(1.0);
    glEnd();
#endif
}

GLOBAL_NAMESPACE_END